#ifndef		_XDPER_H__
#define		_XDPER_H__

/* xdumper dll public used for windows platform
 *
 * created by casey.tq					2015/03/24
 *
 */
// #define char TCHAR
// #define unsigned char TCHAR
#define	DLL_EXPORT

#define	_DLL_VER_			(0x00000002)

/* error code */
#define ERR_OK				(0x00)
#define ERR_CRC				(0x01)
#define ERR_IO				(0x02)
#define ERR_FMT				(0x03)
#define ERR_FSIZE			(0x04)
#define ERR_TIMEOUT			(0x05)
#define ERR_ERASE			(0x06)
#define ERR_WRITE			(0x07)
#define ERR_CMD				(0x08)
#define ERR_ATOM            (0x09)
#define ERR_CONN			(0x0A)
#define ERR_NODEV			(0x0B)
#define ERR_INVAL			(0x10)


#define SIZE(x)				(sizeof (x) / sizeof (x[0]))

/* for upd module ,control flags macro */
#define UPDF_DEFAULT		(0x00010000)		/* default control */
#define UPDF_SRC			(0x00020000)		/* source object is source stream */
#define UPDF_QUIT_IF_NULL	(0x00040000)		/* quit if one of the sources is null */
#define UPDF_GO_THROUGH		(0x00080000)		/* enter the update mode directly ,no need enter xdper module */
#define UPDF_NETDOWNLOAD	(0x00100000)		/* network download */

#define IS_FILENAME(x)		(!((x) & UPDF_SRC))

/* state macro */
#define STAT_CNN			(0x80)				/* connecting device */
#define	STAT_CNN_OK			(0x81)				/* connect OK */
#define STAT_ERASE_FLASH	(0x82)				/* erase flash */
#define STAT_ERASE_DONE		(0x83)				/* erase done */
#define STAT_ERROR			(0x84)				/* error occured */
#define STAT_UPDATING		(0x85)				/* updating */
#define STAT_UPDATE_DONE	(0x86)				/* udpate done */

#define	CB_CMD_START		(0x00)				/* callback start */
#define CB_CMD_DOING		(0x01)				/* callback calling */
#define CB_CMD_END			(0x02)				/* callback end */

#ifdef	__cplusplus
extern	"C" {
#endif
        typedef			unsigned		DPHANDLE;
        typedef			int				(*call_back) (int cmd,int cur,int total,void *arglist);
        typedef			int				(*item_proc) (int cmd,int idx,const char *item,void *arglist) ;

        /* get version api */
        #pragma  pack(push,1)
        typedef struct _devinfo {
                unsigned char   hw_type_major;      /* major hard ware type :
                                                                                         * 00 : 6632
                                                                                         * 01 : 6621
                                                                                         * 02 : 6611
                                                                                         * 03 : 6631
                                                                                         */

                unsigned char   hw_type_minor;      /* minor hard ware type
                                                                                         * for now ,DON'T Care
                                                                                         */

                unsigned short  bldr_ver;           /* boot loader version */
                unsigned char   locked;             /* locked bit */
                unsigned char   sn [13];            /* serial number */
                char            hw_ver [2];         /* hardware version */
                unsigned        app_ver;            /* application version */
                unsigned        lang_ver;           /* language version */

                char			dev_str [64];		/* device name */
        }DEVINFO;
        #pragma pack (pop)

        typedef struct _upd_info {
                DPHANDLE	handle;					/* handle */
                void		*src_tbl	[2];		/* source array */
                int			*quit;					/* quit by force */
                void		*arglist;				/* extral param */
                unsigned	flags;					/* updi flags for control */
                int			(*upd_proc)	 (int stat,unsigned arg1,unsigned arg2,void *arglist);
        } UPDINFO;

        /* Try to connect device and get the handle if succeed. (thread-safe)
         *
         * in param :
         *			ptr_handle		: the pointer of retrieved handle
         *			devinfo			: the information about device
         *			ctl_flags		: the control flags put-in
         *			arglist			: for later used
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	cnn_dev		(DPHANDLE	*ptr_handle,			/* pointer to handle */
                                                                 DEVINFO	*devinfo,				/* device information */
                                                                 unsigned	ctl_flags,				/* control flags */
                                                                 void		*arglist				/* for later used */
                                                                 );


        /* Try to disconnect device. (thread-safe)
         *
         * in param :
         *			handle			: the handle of connection
         *			arglist			: for later used
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	discnn_dev	(DPHANDLE	handle,					/* handle */
                                                                void		*arglist				/* for later used */
                                                                );



        /* To get JUST ONE file from device (thread-safe)
         *        ========
         *
         * in param :
         *			handle			: the handle of connection
         *			remote_path		: file full path at device
         *			local_path		: file full path at local
         *			cb				: called by inner routine every packet
         *			quit			: the pointer of quit flag,if in transferring ,this flag can
         *							: terminates this transmission
         * return	:
         *			>= 0			: the bytes of file we have gotten from device
         *			negative number : the negative error code ,such as :
         *							: if operation succeed ,happy ending ,if failed
         *							: assuming -2 returned ,so the error code is -(-2) , that is error code 2
         *
         */
        DLL_EXPORT	int get_file	(DPHANDLE	handle,					/* device handle */
                                                                 const char *remote_path,			/* remote full path */
                                                                 const char *local_path,			/* local full path */
                                                                 call_back	cb,						/* call back */
                                                                 int		*quit,					/* quit by force */
                                                                 void		*arglist				/* for later used */
                                                                 );



        /* To put JUST ONE file from device (thread-safe)
         *        ========
         *
         * in param :
         *			handle			: the handle of connection
         *			remote_path		: file full path at device
         *			local_path		: file full path at local
         *			cb				: called by inner routine every packet
         *			quit			: the pointer of quit flag,if in transferring ,this flag can
         *							: terminates this transmission
         * return	:
         *			>= 0			: the bytes of file we have put into device
         *			negative number : the negative error code ,such as :
         *							: if operation succeed ,happy ending ,if failed
         *							: assuming -2 returned ,so the error code is -(-2) , that is error code 2
         *
         */
        DLL_EXPORT	int put_file	(DPHANDLE	handle,					/* device handle */
                                                                 const char *remote_path,			/* remote full path */
                                                                 const char *local_path,			/* local full path */
                                                                 call_back	cb,						/* call back */
                                                                 int		*quit,					/* quit by force */
                                                                 void		*arglist				/* for later used */
                                                                 );



        /* To remove JUST ONE file from device (thread-safe)
         *           ========
         *
         * in param :
         *			handle			: the handle of connection
         *			remote_path		: file full path at device
         *			arglist			: for later used
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	rm_file		(DPHANDLE	handle,					/* handle */
                                                                 const char *remote_path,			/* remote full path */
                                                                 void		*arglist				/* for later used */
                                                                 );



        /* To get all file list file from device (thread-safe)
         *
         * in param :
         *			handle			: the handle of connection
         *			proc			: the item proc
         *			arglist			: for later used
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	get_list	(DPHANDLE	hdev,					/* handle */
                                                                 item_proc	proc,					/* list item proc */
                                                                 void		*arglist				/* for later used */
                                                                 );


        /* To ping the device (thread-safe)
         *
         * in param :
         *			handle			: the handle of connection
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	ping_dev	(DPHANDLE	handle,					/* handle */
                                                                 void		*arglist				/* for later used */
                                                                 );


        /* To update the firmware of device (thread-safe)
         *
         * in param :
         *			handle			: the handle of connection
         * return	:
         *			0 if happy
         *			none-zero for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	upd_dev		(DPHANDLE	*handle,				/* handle */
                                                                 UPDINFO	*updi,					/* update info */
                                                                 void		*arglist				/* for later used */
                                                                 );


        /* Try to get the work mode of device if succeed. (thread-safe)
         *
         * in param :
         *			ptr_handle		: the pointer of retrieved handle
         *		    arglist			: later used
         * return	:
         *			>= 0 number  means the work mode if happy
         *			< 0 number for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	get_dev_workmode	   (DPHANDLE	handle,				/* pointer to handle */
                                                                                        void		*arglist			/* for later use d */
                                                                                        );


        /* Try to get the filecnt of device if succeed. (thread-safe)
         *
         * in param :
         *			ptr_handle		: the pointer of retrieved handle
         *		    arglist			: later used
         * return	:
         *			>= 0 number  means the file count if happy
         *			< 0 number for error code ,for detail ,look up error code above
         *
         */
        int DLL_EXPORT	get_filecnt				(DPHANDLE	handle,					/* pointer to handle */
                                                                                        void		*arglist				/* for later use d */
                                                                                        );

        /* Try to detect the xdper device. (thread-safe)
         *
         * in param :
         *			ctl_flags		: the control flags put-in
         * return	:
         *			1 if happy
         *			0 if failed
         *
         */
        int	DLL_EXPORT	detect_xdp_dev (void *arglist);

#ifdef	__cplusplus
}
#endif

#endif
